package com.example.opengldemo._11_;

import android.content.Context;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;

import com.example.opengldemo.R;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class TextureView extends GLSurfaceView implements GLSurfaceView.Renderer {
    TextureGraphics texture1111 ;
    TextureGraphics texture2222 ;
    public static int SCREEN_WIDTH = 800;
    public static int SCREEN_HEIGHT = 480;
    public static int WIDTH_ITEM  ;
    public static int HEIGHT_ITEM  ;
    public TextureView(Context context) {
        super(context);
        initConfig();
    }
    static int rectState = KeyThread.Stop;
    static final float moveSpan = 0.1f;
    private KeyThread keyThread;

    private void initConfig() {
        this.setEGLContextClientVersion(3);
        this.setRenderer(this);
        this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }
    @Override
    public boolean onTouchEvent(MotionEvent e)
    {
        float y = e.getY();
        float x = e.getX();

        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.e("textureView" , x +"==" + y) ;
                Log.e("textureView2" , SCREEN_WIDTH +"==" + SCREEN_WIDTH) ;
                int a = 0 ;
                if(x<WIDTH_ITEM) {
                    a = 1;
                }else if(x>2*WIDTH_ITEM){
                    a = 100;
                }else{
                    a = 10;
                }
                int b = 0 ;
                if(y<HEIGHT_ITEM) {
                    b = 1000;
                }else if(y>2*HEIGHT_ITEM){
                    b = 100000;
                }else{
                    b = 10000;
                }
                rectState = a+b ;
                Log.e("textureView" , x +"==" + y) ;
                Log.e("textureView2" , SCREEN_WIDTH +"==" + SCREEN_WIDTH +"==="+rectState) ;
                break;
            case MotionEvent.ACTION_UP://Ãß∆ ±Õ£÷π“∆∂Ø
                rectState = KeyThread.Stop;
                break;
        }
        return true;
    }
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        GLES30.glClearColor(0.5f  , 0.5f , 0.5f , 1.0f);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        GLES30.glDisable(GLES30.GL_CULL_FACE);

        texture1111 = new TextureGraphics(this);
        texture2222 = new TextureGraphics(this) ;
        MatrixState.setInitStack();

    }
    int textureID2 ;
    int textureID1 ;
    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        GLES30.glViewport( 0 , 0 , i , i1);
        SCREEN_WIDTH = i ;
        SCREEN_HEIGHT = i1 ;

        WIDTH_ITEM = i /3 ;
        HEIGHT_ITEM = i1 /3 ;

        float ratio= (float) i / i1;
        MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 1f, 10f);
        MatrixState.setCamera(0f,0f,5f,0f,0f,0f,0f,1.0f,0.0f);
        textureID2 =  TextureUtil.getTextureId(this , R.drawable.was2 , null, false) ;
        textureID1 =  TextureUtil.getTextureId(this , R.drawable.was , null, false) ;
        keyThread = new KeyThread(this);
        keyThread.start();

    }
    static float tranX;
    static float tranY;
    static float tranZ ;

    static float rotateX ;
    static float rotateY ;
    static float rotateZ ;


    static int srcTextureState  = GLES30.GL_SRC_COLOR;
    static int postTextuteState = GLES30.GL_ONE_MINUS_SRC_COLOR;

    static int equation = GLES30.GL_FUNC_ADD ;
    @Override
    public void onDrawFrame(GL10 gl10) {
        GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
        MatrixState.pushMatrix();

        MatrixState.pushMatrix();
        texture1111.drawSelf(textureID2);
        MatrixState.popMatrix();

        GLES30.glEnable(GLES30.GL_BLEND);
        GLES30.glBlendEquation(equation);
        GLES30.glBlendFunc(srcTextureState, postTextuteState);
        MatrixState.pushMatrix();
        Log.e("ondraw" , tranX + "=="+tranY +"=="+tranZ);
        MatrixState.translate(tranX, tranY, tranZ);
       // MatrixState.setCamera(rotateX , rotateY , rotateZ , 0 , 0 , 0 , 0 ,1, 0);
        texture2222.drawSelf(textureID1);//ªÊ÷∆¬Àπ‚æµŒ∆¿Ìæÿ–Œ
        MatrixState.popMatrix();
        //πÿ±’ªÏ∫œ
        GLES30.glDisable(GLES30.GL_BLEND);

        MatrixState.popMatrix();

    }



}
